﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CluelessGame
{
    public class CardFactory
    {
        public void DealCards(Player[] players, int activePlayerCount, 
            out Dictionary<Player, List<Card>> hands, out CrimeCards crimeCards)
        {
            hands = null;
            crimeCards = null;

            hands = new Dictionary<Player, List<Card>>();
            for (int i = 0; i < activePlayerCount; i++)
            {
                hands[players[i]] = new List<Card>();
            }

            List<CharacterCard> characterCards = new List<CharacterCard>();
            List<RoomCard> roomCards = new List<RoomCard>();
            List<WeaponCard> weaponCards = new List<WeaponCard>();

            characterCards.Add(new CharacterCard(GameManager.CurrentGameObjects.ColonelMustard));
            GameManager.CurrentGameObjects.ColonelMustard.AssociatedCard = characterCards.Last();
            characterCards.Add(new CharacterCard(GameManager.CurrentGameObjects.MissScarlet));
            GameManager.CurrentGameObjects.MissScarlet.AssociatedCard = characterCards.Last();
            characterCards.Add(new CharacterCard(GameManager.CurrentGameObjects.MrGreen));
            GameManager.CurrentGameObjects.MrGreen.AssociatedCard = characterCards.Last();
            characterCards.Add(new CharacterCard(GameManager.CurrentGameObjects.MsPeakcock));
            GameManager.CurrentGameObjects.MsPeakcock.AssociatedCard = characterCards.Last();
            characterCards.Add(new CharacterCard(GameManager.CurrentGameObjects.MsWhite));
            GameManager.CurrentGameObjects.MsWhite.AssociatedCard = characterCards.Last();
            characterCards.Add(new CharacterCard(GameManager.CurrentGameObjects.ProfessorPlum));
            GameManager.CurrentGameObjects.ProfessorPlum.AssociatedCard = characterCards.Last();

            roomCards.Add(new RoomCard(GameManager.CurrentGameObjects.Ballroom));
            GameManager.CurrentGameObjects.Ballroom.AssociatedCard = roomCards.Last();
            roomCards.Add(new RoomCard(GameManager.CurrentGameObjects.Billiardroom));
            GameManager.CurrentGameObjects.Billiardroom.AssociatedCard = roomCards.Last();
            roomCards.Add(new RoomCard(GameManager.CurrentGameObjects.Conservatory));
            GameManager.CurrentGameObjects.Conservatory.AssociatedCard = roomCards.Last();
            roomCards.Add(new RoomCard(GameManager.CurrentGameObjects.Diningroom));
            GameManager.CurrentGameObjects.Diningroom.AssociatedCard = roomCards.Last();
            roomCards.Add(new RoomCard(GameManager.CurrentGameObjects.Hall));
            GameManager.CurrentGameObjects.Hall.AssociatedCard = roomCards.Last();
            roomCards.Add(new RoomCard(GameManager.CurrentGameObjects.Kitchen));
            GameManager.CurrentGameObjects.Kitchen.AssociatedCard = roomCards.Last();
            roomCards.Add(new RoomCard(GameManager.CurrentGameObjects.Library));
            GameManager.CurrentGameObjects.Library.AssociatedCard = roomCards.Last();
            roomCards.Add(new RoomCard(GameManager.CurrentGameObjects.Lounge));
            GameManager.CurrentGameObjects.Lounge.AssociatedCard = roomCards.Last();
            roomCards.Add(new RoomCard(GameManager.CurrentGameObjects.Study));
            GameManager.CurrentGameObjects.Study.AssociatedCard = roomCards.Last();

            weaponCards.Add(new WeaponCard(GameManager.CurrentGameObjects.Candlestick));
            GameManager.CurrentGameObjects.Candlestick.AssociatedCard = weaponCards.Last();
            weaponCards.Add(new WeaponCard(GameManager.CurrentGameObjects.Knife));
            GameManager.CurrentGameObjects.Knife.AssociatedCard = weaponCards.Last();
            weaponCards.Add(new WeaponCard(GameManager.CurrentGameObjects.Pipe));
            GameManager.CurrentGameObjects.Pipe.AssociatedCard = weaponCards.Last();
            weaponCards.Add(new WeaponCard(GameManager.CurrentGameObjects.Revolver));
            GameManager.CurrentGameObjects.Revolver.AssociatedCard = weaponCards.Last();
            weaponCards.Add(new WeaponCard(GameManager.CurrentGameObjects.Rope));
            GameManager.CurrentGameObjects.Rope.AssociatedCard = weaponCards.Last();
            weaponCards.Add(new WeaponCard(GameManager.CurrentGameObjects.Wrench));
            GameManager.CurrentGameObjects.Wrench.AssociatedCard = weaponCards.Last();

            crimeCards = new CrimeCards();
            crimeCards.Character = characterCards[App.Rand.Next() % characterCards.Count];
            characterCards.Remove(crimeCards.Character);
            crimeCards.Room = roomCards[App.Rand.Next() % roomCards.Count];
            roomCards.Remove(crimeCards.Room);
            crimeCards.Weapon = weaponCards[App.Rand.Next() % weaponCards.Count];
            weaponCards.Remove(crimeCards.Weapon);                
                
            List<Card> undealtCards = new List<Card>();

            undealtCards.AddRange(characterCards);
            undealtCards.AddRange(roomCards);
            undealtCards.AddRange(weaponCards);

            int currentPlayer = 0;
            while (undealtCards.Count > 0)
            {
                Card selectedCard = undealtCards[App.Rand.Next() % undealtCards.Count];
                hands[players[currentPlayer]].Add(selectedCard);
                undealtCards.Remove(selectedCard);

                currentPlayer++;
                if (currentPlayer >= activePlayerCount)
                {
                    currentPlayer = 0;
                }
            }
        }
    }
}
